package edu.scau.divineplace.entity.enums;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonValue;

/**
 * @Author: x1aolone
 * @Date: 2020/12/30 15:46
 * 枚举基类
 */
public interface BaseEnum {


    /**
     * 获取枚举集合
     * @param clazz
     * @return
     */
    static <T extends BaseEnum> T[] values(Class<T> clazz) {
        if (!clazz.isEnum()) {
            throw new IllegalArgumentException("Class[" + clazz + "]不是枚举类型");
        }
        //得到values
        return clazz.getEnumConstants();
    }


    /**
     * 获取枚举 区分大小写
     *
     * @param clazz
     * @param type
     * @return
     */
    static <T extends BaseEnum> T typeOf(Class<T> clazz, Integer type) {
        return typeOf(clazz, type, false);
    }

    /**
     * 获取枚举 按需区分大小写
     * @param clazz
     * @param type
     * @param ignoreCase
     * @return
     */
    static <T extends BaseEnum> T typeOf(Class<T> clazz, Integer type, boolean ignoreCase) {
        //得到values
        T[] enums = values(clazz);
        if (enums == null || enums.length == 0) {
            return null;
        }
        for (T t : enums) {
            if (ignoreCase && t.getType().equals(type)) {
                return t;
            } else if (t.getType().equals(type)) {
                return t;
            }
        }
        return null;
    }

    /**
     * 是否存在对应的枚举
     * @param clazz
     * @param type
     * @return
     */
    static <T extends BaseEnum> boolean isExist(Class<T> clazz, Integer type) {
        return typeOf(clazz, type, false) != null;
    }

    /**
     * 获取type
     *
     * @return
     */
    Integer getType();
}
